import sqlite3
import json

try:
    with open(r"C:\\Users\\P02PUBLIC\\Downloads\\chinese-poetry-master\\御定全唐詩\\json\\001.json", "r", encoding="utf-8") as file:
        data = json.load(file)
        # 连接到 SQLite 数据库（如果不存在则创建）
        conn = sqlite3.connect("C:\\Users\\P02PUBLIC\\Downloads\\chinese-poetry-master\\Ancient_Chinese_poetry.sqlite3")
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM main")
        table_exists = cursor.fetchone()
        print("Table exists:", table_exists)
        # 插入数据
        cursor.execute("""
            INSERT INTO `main` ("id","book","preface","volume","part","chapter",
            "subchapter","section","subsection","type","title","subtitle",
            "rhythmic","subrhythmic","no#","author","biography","prologue",
            "short_description","description","abstract","source","dynasty",
            "paragraphs","contents","translation","notes","comments","tags",
            "spells","strains","origin")
            VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """, (
            ["book"],
            ["preface"],
            ["volume"],
            ["part"],
            ["chapter"],
            ["subchapter"],
            ["section"],
            ["subsection"],
            ["type"],
            data[0]["title"],
            ["subtitle"],
            ["rhythmic"],
            ["subrhythmic"],
            ["no#"],
            ["author"],
            ["biography"],
            ["prologue"],
            ["short_description"],
            ["description"],
            ["abstract"],
            ["source"],
            ["dynasty"],
            ["paragraphs"],
            ["contents"],
            ["translation"],
            ["notes"],
            ["comments"],
            ["tags"],
            ["spells"],
            ["strains"],
            ["origin"]

))

        # 提交事务并关闭连接
        conn.commit()
        conn.close()
        #print(data[1]['author'])
except json.JSONDecodeError as e:
    print("JSON 文件格式错误:", e)
except FileNotFoundError:
    print("文件未找到")

